﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using OnlineManagement.BLL;
namespace OnlineManagement.Web.pages
{
    public partial class PigletUpdate : System.Web.UI.Page
    {
        protected string _phaseFromID = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack){
                InitPageControl();
            }
        }

        
        private void InitPageControl() {
            try
            {
                BindDropDownlist(1001);
                string sowIdStr = Request.QueryString["sowId"] ?? string.Empty;
                string pigletIDStr = Request.QueryString["letId"] ?? string.Empty;
                string bitId = Request.QueryString["bitID"] ?? string.Empty;
                string phaseName = Request.QueryString["phaseName"] ?? string.Empty;
                string customizeID = Request.QueryString["customizeID"] ?? string.Empty;
                string pigType = Request.QueryString["pigType"] ?? string.Empty;
                string birthday = Request.QueryString["birthday"] ?? string.Empty;
                string account = Request.QueryString["account"] ?? string.Empty;
                string castrateDate = Request.QueryString["castrateDate"] ?? string.Empty;
                //phaseName = "保育期";
                hfPigletID.Value = pigletIDStr;
                lbBitID.Text = bitId;
                lbPigPenId.Text = customizeID;
                lbBirthday.Text = birthday;
                lbPhaseName.Text = phaseName;
                lbPigType.Text =string.IsNullOrEmpty(pigType)?"未设置":pigType;
                lbTotalAccount.Text = account;
                if (!string.IsNullOrEmpty(castrateDate))
                {
                    cbIsCastrate.Checked = true;
                    cbNoCastrate.Checked = false;
                    //pCastrate.Visible = true;
                    txtCastrateDate.Text = castrateDate;
                }
                else {
                    cbIsCastrate.Checked = false;
                    cbNoCastrate.Checked = true;
                    //pCastrate.Visible = false;
                }

                ddlPhaseToID.SelectedValue = ddlPhaseToID.Items.FindByText(phaseName).Value;
                _phaseFromID = ddlPhaseToID.Items.FindByText(phaseName).Value;
                hfPhaseToID.Value = _phaseFromID;
            }catch(Exception){
                
            }
        }
        private void BindDropDownlist(int hoggeryId) {
            try
            {
                string cachKey = OnlineManagement.Common.PubConstant.CacheHogBaseInfoName;
                object obj = OnlineManagement.Common.DataCache.GetCache(cachKey);
                DataSet ds;
                if (obj != null && obj is DataSet)
                {
                    //return 3 tables,1:PigSource,2:PropagateState,3:PigBreed,4:PigPen,5:pigCategory,6:GrowPhase
                    ds = obj as DataSet;
                }
                else
                {
                    ProcedureHandle handle = new ProcedureHandle();
                    ds = handle.GetPigBaseInfoProcedure(hoggeryId);
                }
                Bind(ds);
            }catch(Exception ex){
                throw ex;
            }
        }

        private void Bind(DataSet ds)
        {
            if (ds != null && ds.Tables != null && ds.Tables.Count > 0)
            {
                ddlPhaseToID.DataSource = ds.Tables[5];
                ddlPhaseToID.DataTextField = "Name";
                ddlPhaseToID.DataValueField = "ID";
                ddlPhaseToID.DataBind();

                ddlPigType.DataSource = ds.Tables[2];
                ddlPigType.DataTextField = "Name";
                ddlPigType.DataValueField = "ID";
                ddlPigType.DataBind();

                ddlPigPen.DataSource = ds.Tables[3];
                ddlPigPen.DataTextField = "Name";
                ddlPigPen.DataValueField = "ID";
                ddlPigPen.DataBind();

                
            }
        }
    }
}